<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="方案名称" prop="planName">
          <el-input
            v-model="queryParams.planName"
            placeholder="请输入方案名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车型名称" prop="modelName">
          <el-input
            v-model="queryParams.modelName"
            placeholder="请输入车型名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否启用" prop="isAvailable">
          <el-select
            v-model="queryParams.isAvailable"
            clearable
            @1clear="handleQuery"
            style="width: 220px"
          >
            <template v-for="item in dict.type.sys_available">
              <el-option
                :label="item.label"
                :value="item.value"
                :key="item.value"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            class="first-plain-btn"
            size="medium"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['longOrder:plan:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            class="sec-plain-btn"
            size="medium"
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['longOrder:plan:remove']"
            >删除</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="planList"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column
          label="方案名称"
          align="center"
          prop="planName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="车型信息"
          align="center"
          prop="modelInfo"
          min-width="240"
          show-overflow-tooltip
        >
          <template slot-scope="{row}">
            {{row.modelInfo+'/'+row.modelName}}
          </template>
        </el-table-column>
        <el-table-column
          label="车辆押金/元"
          align="center"
          prop="deposit"
          min-width="240"
          show-overflow-tooltip
        />
        <el-table-column
          label="租金/元"
          align="center"
          prop="monthlyRent"
          min-width="240"
          show-overflow-tooltip
        />
        <el-table-column
          label="创建时间"
          align="center"
          prop="updateTime"
          min-width="160"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="所属门店"
          align="center"
          prop="deptName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="所属租户"
          align="center"
          prop="tenantName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="是否启用"
          align="center"
          prop="isAvailable"
          min-width="80"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_available"
              :value="scope.row.isAvailable"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="220"
          fixed="left"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDetail(scope.row)"
              v-hasPermi="['longOrder:plan:edit']"
              >详情</el-button
            >
            <template v-if="scope.row.isAvailable == 0">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['longOrder:plan:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"
                v-hasPermi="['longOrder:plan:remove']"
                >删除</el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="handleAvailable(scope.row)"
                v-hasPermi="['longOrder:plan:edit']"
                >启用</el-button
              >
            </template>
            <el-button
              v-if="scope.row.isAvailable == 1"
              size="mini"
              type="text"
              @click="handleAvailable(scope.row)"
              v-hasPermi="['longOrder:plan:edit']"
              >停用</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改长租方案主对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
      @close="nowId = null"
    >
      <MmvAdd ref="MmvAdd" v-if="open" :id="nowId"></MmvAdd>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="详情"
      :visible.sync="opendetail"
      width="1000px"
      append-to-body
      @close="nowId = null"
    >
      <MmvDetail ref="MmvDetail" v-if="opendetail" :id="nowId"></MmvDetail>
      <div slot="footer" class="dialog-footer">
        <el-button @click="opendetail = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPlan,
  getPlan,
  delPlan,
  addPlan,
  updatePlan,
  exportPlan,
  stopUsingEnabled,
} from '@/api/longPlan/plan'
import MmvAdd from './add.vue'
import MmvDetail from './detail.vue'

export default {
  name: 'Plan',
  components: {
    MmvAdd,
    MmvDetail,
  },
  dicts: ['sys_available'],
  data() {
    return {
      opendetail: false,
      nowId: null,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 长租方案主表格数据
      planList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        longPlanName: null,
        modelId: null,
        modelName: null,
        modelInfo: null,
        configurationId: null,
        paymentSupport: null,
        deposit: null,
        monthlyRent: null,
        isAvailable: null,
        deptId: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询长租方案主列表 */
    getList() {
      this.loading = true
      listPlan(this.queryParams).then((response) => {
        this.planList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        longPlanName: null,
        modelId: null,
        modelName: null,
        modelInfo: null,
        configurationId: null,
        paymentSupport: null,
        deposit: null,
        monthlyRent: null,
        delFlag: null,
        isAvailable: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.planId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加月租方案'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true
      this.nowId = row.planId
      this.title = '修改月租方案'
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.opendetail = true
      this.nowId = row.planId
    },
    handleAvailable(row) {
      const id = row.planId
      var tip = '停用'
      var isAvailable = 0

      if (row.isAvailable == 0) {
        tip = '启用'
        var isAvailable = 1
      }

      this.$modal
        .confirm('是否确认' + tip + '方案【' + row.planName + '】？')
        .then(function () {
          var data = {
            longPlanId: id,
            isAvailable: isAvailable,
          }
          return stopUsingEnabled(data)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('操作成功')
        })
        .catch(() => {})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.MmvAdd.emitData().then((res) => {
        if (this.nowId) {
          updatePlan(res).then((response) => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        } else {
          addPlan(res).then((response) => {
            this.$modal.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
      // this.$refs["form"].validate(valid => {
      //     if (valid) {
      //         if (this.form.planId != null) {
      //             updatePlan(this.form).then(response => {
      //                 this.$modal.msgSuccess("修改成功");
      //                 this.open = false;
      //                 this.getList();
      //             });
      //         } else {
      //             addPlan(this.form).then(response => {
      //                 this.$modal.msgSuccess("新增成功");
      //                 this.open = false;
      //                 this.getList();
      //             });
      //         }
      //     }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids
      this.$modal
        .confirm('是否确认删除数据项？')
        .then(function () {
          return delPlan(planIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有长租方案主数据项？')
        .then(() => {
          this.exportLoading = true
          return exportPlan(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
